// #ifdef H5
import { mJiGuangLogin } from '@/api/login';
import { tdkUrl } from '@/common/common'
export const JVinit = () => {
  window.JVerificationInterface.init({
    appkey: '2f4f51d51341427e1aa1f934', // 极光官网中创建应用后分配的 appkey，必填   2f4f51d51341427e1aa1f934
    debugMode: true, // 设置是否开启 debug 模式。true 则会打印更多的日志信息。设置 false 则只会输出 w、e 级别的日志
    fail: function(data) {
      console.log('初始化失败:' + JSON.stringify(data))
    }, success: function(data) {
      console.log('初始化成功:' + JSON.stringify(data))
      getToken()
    } })
}

export const getToken = () => {
  // 获取运营商token，默认尝试顺序：移动>联通>电信
  // 实际使用中建议在初始化成功回掉中调用此接口
  window.JVerificationInterface.getToken({

    fail: function(data) {
      uni.$u.toast('使用其他登录方式登录')
      window.location.href = tdkUrl.develop + '/front/login/login/login'
    }, success: function(data) {
      var token = data.content;
      loginAuth()
      console.log('getToken:' + token)
    }
  })
}
export const loginAuth = () => {
  // 一键登录接口，默认尝试顺序：移动>联调>电信    operater:"CU",
  // 实际使用中建议在初始化成功回掉中调用此接口
  window.JVerificationInterface.loginAuth({

    fail: function(data) {
      console.log('fail:' + JSON.stringify(data))
    }, success: function(data) {
      var token = data.content;
      const params = {
        loginToken: token,
        platform: 'M站',
        channelSource: 10,
        registerPlatform: 84,
        device_platform: 'h5'
      }
      mJiGuangLogin(params).then((res) => {
        if (Number(res.code) != 0) {
          uni.$u.toast('获取用户信息失败')
          return
        }
        uni.$u.vuex('vuex_loginStatus', true)
        uni.$u.vuex('vuex_memberInfo', res.memberInfo)
        uni.$u.vuex('vuex_memberCompanyInfo', res.memberCompanyInfo)
        uni.$u.vuex('vuex_crmCustomerInfos', res.crmCustomerInfos)
        uni.$u.vuex('vuex_memberLevelResp', res.memberLevelResp)
        uni.$u.vuex('vuex_mainCustomerSetupAndExtFront', res.mainCustomerSetupAndExtFront)
        // #ifdef H5
        this.$cookie.set('memberId', res.memberInfo.memberId, 365)
        this.$cookie.set('loginAccount', res.memberInfo.loginAccount, 365)
        this.$cookie.set('accountType', res.memberInfo.accountType, 365)
        this.$cookie.set('platformName', res.memberInfo.platformType, 365)
        this.$cookie.set('sourceUrl', encodeURIComponent(window.location.href), 1)
        this.$cookie.set('lgdomain', this.$cookie.getUrlDomain(), 365);
        this.$cookie.set('registerSource', res.memberInfo.registerSource, 365);
        // #endif
        window.location.href = tdkUrl.develop + '/front/pages/myAccount/index'
      }).catch(() => {
        // eslint-disable-next-line no-mixed-spaces-and-tabs
      })
    // window.location.href = 'https://m2.xfs.com/#/shopIndex/shopIndex'
    }
  })
}
//  #endif
